package filter

import (
	"github.com/beego/beego/v2/server/web/context"
	"ksd-social-api/commons/global"
	"net/http"
)

var success = []byte("SUPPORT OPTIONS")

func CorsFilter(ctx *context.Context) {
	global.SugarLog.Infof("1: 拦截器 %s 进来了...", "CorsFilter")
	//跨域设置
	ctx.Output.Header("Access-Control-Allow-Origin", "*")
	//允许访问源
	ctx.Output.Header("Access-Control-Allow-Methods", "OPTIONS,DELETE,POST,GET,PUT,PATCH")
	ctx.Output.Header("Access-Control-Max-Age", "3600")
	//允许post访问
	ctx.Output.Header("Access-Control-Allow-Headers", "Access-Control-Allow-Headers,Access-Control-Allow-Origin,ContentType,Authorization,accept,accept-encoding, authorization, content-type")
	ctx.Output.Header("Access-Control-Allow-Credentials", "true")
	if ctx.Input.Method() == http.MethodOptions {
		// options请求，返回200
		ctx.Output.SetStatus(http.StatusOK)
		_ = ctx.Output.Body(success)
	}
}
